package com.jeesite.modules.fl.web;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.fl.entity.ClinicSetting;
import com.jeesite.modules.fl.entity.Clinic;
import com.jeesite.modules.fl.service.ClinicService;
import com.jeesite.modules.fl.service.ClinicSettingService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;

@Controller
@RequestMapping(value = "${adminPath}/fl/clinicsetting")
public class ClinicSettingController extends BaseController{
	
	@Autowired
	private ClinicSettingService clinicSettingService;
	@Autowired
	private ClinicService clinicService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ClinicSetting get(String id, boolean isNewRecord) {
		return clinicSettingService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("fl:clinicsetting:view")
	@RequestMapping(value = {"list", ""})
	public String list(ClinicSetting clinicSetting, HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("clinicSetting", clinicSetting);
		User user = UserUtils.getUser();
		Clinic entity = new Clinic();
        entity.setStatus("0");
        if(!user.isAdmin()){
        	entity.getSqlMap().put("dsf", " and (EXISTS (SELECT 1 FROM fl_clinic_user WHERE users like '%"+user.getId()+"%' AND clinic=a.id) or EXISTS (SELECT 1 FROM js_sys_user WHERE user_code='"+user.getId()+"' AND user_code=a.create_by))");
		}
        List<Clinic> clinicList = clinicService.findList(entity);
        model.addAttribute("clinicList", clinicList);
		return "modules/fl/clinicSettingList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("fl:clinicsetting:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ClinicSetting> listData(ClinicSetting clinicSetting, HttpServletRequest request, HttpServletResponse response) {
		User user = UserUtils.getUser();
		if(!user.isAdmin()){
			clinicSetting.getSqlMap().put("dsf", " and (EXISTS (SELECT 1 FROM fl_clinic_user WHERE users like '%"+user.getId()+"%' AND clinic=c.id) or EXISTS (SELECT 1 FROM js_sys_user WHERE user_code='"+user.getId()+"' AND user_code=c.create_by))");
		}
		Page<ClinicSetting> page = clinicSettingService.findPage(new Page<ClinicSetting>(request, response), clinicSetting);
		return page;
	}
	
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("fl:clinicsetting:view")
	@RequestMapping(value = "form")
	public String form(ClinicSetting clinicSetting, Model model) {
		clinicSetting.setClinic(clinicService.get(clinicSetting.getClinic()));
		model.addAttribute("clinicSetting", clinicSetting);
		return "modules/fl/clinicSettingForm";
	}
	
	@RequiresPermissions("fl:clinicsetting:view")
	@RequestMapping(value = "formAll")
	public String formAll(String clinicId, Model model) {
		ClinicSetting clinicSetting = new ClinicSetting();
		clinicSetting.setClinic(clinicService.get(clinicId));
		model.addAttribute("clinicSetting", clinicSetting);
		return "modules/fl/clinicSettingFormAll";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("fl:clinicsetting:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ClinicSetting clinicSetting) {
		clinicSettingService.save(clinicSetting);
		return renderResult(Global.TRUE, text("保存数据成功！"));
	}
	
	@RequiresPermissions("fl:clinicsetting:edit")
	@PostMapping(value = "saveAll")
	@ResponseBody
	public String saveAll(@Validated ClinicSetting clinicSetting) {
		ClinicSetting entity = new ClinicSetting();
		entity.setClinic(clinicSetting.getClinic());
		List<ClinicSetting> list = clinicSettingService.findList(entity);
		for(ClinicSetting cs : list){
			cs.setBusinessHours(clinicSetting.getBusinessHours());
			cs.setNumber(clinicSetting.getNumber());
			cs.setServiceTime(clinicSetting.getServiceTime());
			clinicSettingService.save(cs);
		}
		return renderResult(Global.TRUE, text("批量设置数据成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("fl:clinicsetting:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(ClinicSetting clinicSetting) {
		clinicSetting.setStatus(ClinicSetting.STATUS_DISABLE);
		clinicSettingService.updateStatus(clinicSetting);
		return renderResult(Global.TRUE, text("停用数据成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("fl:clinicsetting:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(ClinicSetting clinicSetting) {
		clinicSetting.setStatus(ClinicSetting.STATUS_NORMAL);
		clinicSettingService.updateStatus(clinicSetting);
		return renderResult(Global.TRUE, text("启用数据成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("fl:clinicsetting:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ClinicSetting clinicSetting) {
		clinicSettingService.delete(clinicSetting);
		return renderResult(Global.TRUE, text("删除数据成功！"));
	}

}
